package goods

import (
	"github.com/gin-gonic/gin"
	"shop/formdata"
	"shop/internal/controller/base"
	goods2 "shop/internal/server/goods"
)

// 商品详情
func GoodsSearchTypeByGoodsTitle(c *gin.Context) {
	var goodsInfoById *formdata.SearchByGoodsId
	if err := c.ShouldBindJSON(&goodsInfoById); err != nil {
		base.RespData(c, nil, err)
		return
	}
	/*	if goodsInfoById.GoodsID == 0 {
		base.RespData(c, nil, fmt.Errorf("商品ID不能为空"))
		return
	}*/
	goodsInfo, err := goods2.GetGoodsInfo(goodsInfoById.GoodsID)
	if err != nil {
		base.RespData(c, nil, err)
		return
	}
	base.RespData(c, goodsInfo, nil)
}

// 商品列表
func GoodsList(c *gin.Context) {
	var goodsListById formdata.GoodsList
	if err := c.ShouldBindJSON(&goodsListById); err != nil {
		base.RespData(c, nil, err)
		return
	}
	goodsList, err := goods2.ListByGoodsIds(goodsListById)
	if err != nil {
		return
	}

	base.RespData(c, goodsList, nil)
}

// 商品分类
func GetGoodsIDs(c *gin.Context) {
	goodsTypes, err := goods2.GoodsTypes()
	if err != nil {
		return
	}

	base.RespData(c, goodsTypes, nil)
}

// 商品等级
func GetTypesInfo(c *gin.Context) {
	var typesInfo formdata.TypesInfo
	if err := c.ShouldBindJSON(&typesInfo); err != nil {
		base.RespData(c, nil, err)
		return
	}
	/*	if typesInfo.Location < 0 {
		base.RespData(c, nil, fmt.Errorf("不能为空"))
		return
	}*/

	typesLervel, err := goods2.GetTypesLervel(typesInfo.Location)
	if err != nil {
		return
	}

	base.RespData(c, typesLervel, nil)
}
